import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:toys_flutter/pages/EditDynamic.dart';
import 'package:toys_flutter/pages/user/MyDongTai.dart';
import 'package:toys_flutter/pages/user/MyFenSi.dart';
import 'package:toys_flutter/pages/user/MyGuanZhu.dart';

class BlockData extends StatelessWidget {
  final Map<String, dynamic>? userData; // 存储用户数据的状态变量
  const BlockData({super.key, this.userData});

  // 数字大小
  static const double mainFontSize = 16;

  // 文字大小
  static const double mainFontSize2 = 14;
  static const double subFontSize = 10;

  // 颜色
  static const List<Color> colors = [
    Color.fromRGBO(135, 232, 222, 1),
    Color.fromRGBO(255, 173, 210, 1),
    Color.fromRGBO(183, 235, 143, 1),
  ];

  List<Map<String, dynamic>> _generateCardData() {
    return [
      {
        'num': userData?['community'] ?? 0,
        'title': '我的动态',
        'subtitle': '珍惜每一个平凡的瞬间',
        'buttonText': '发动态',
        'buttonBgColor': Colors.red,
        'buttonColor': Colors.white,
        'page': const MyDongTai()
      },
      {
        'num': userData?['flow'] ?? 0,
        'title': '关注',
        'subtitle': '期待每一次热情的关注',
        'buttonText': '去查看',
        'buttonBgColor': Colors.white,
        'buttonColor': Colors.black87,
        'page': const MyGuanZhu(),
      },
      {
        'num': userData?['p_flow'] ?? 0,
        'title': '粉丝',
        'subtitle': '感谢每一份默默的支持',
        'buttonText': '去看看',
        'buttonBgColor': Colors.white,
        'buttonColor': Colors.black87,
        'page': const MyFenSi(),
      },
    ];
  }

  // 创建卡片小部件
  Widget buildCard(
      Map<String, dynamic> cardData, int index, BuildContext context) {
    return Expanded(
      child: InkWell(
        onTap: () {
          if (cardData['page'] != null) {
            Get.to(cardData['page']);
          }
        },
        child: Container(
          margin: const EdgeInsets.all(5),
          padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(10)),
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                colors[index].withOpacity(0.3),
                colors[index],
                colors[index].withOpacity(0.3),
              ],
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('${cardData['num']}',
                  style: const TextStyle(fontSize: mainFontSize)),
              Text(cardData['title'],
                  style: const TextStyle(fontSize: mainFontSize2)),
              Container(
                alignment: Alignment.topLeft,
                child: Text(cardData['subtitle'],
                    style: const TextStyle(
                        fontSize: subFontSize, color: Colors.black38)),
              ),
              Center(
                child: TextButton(
                  style: TextButton.styleFrom(
                    foregroundColor: Colors.white,
                    backgroundColor: cardData['buttonBgColor'], // 按钮背景颜色
                    padding:
                        const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
                    minimumSize: const Size(0, 0), // 限制按钮的最小尺寸
                  ),
                  onPressed: () {
                    if (cardData['page'] != null) {
                      Get.to(cardData['page']);
                    } else {
                      // 跳转到发动态页面
                      Get.to(const EditDynamicPage());
                    }
                  },
                  child: Text(
                    cardData['buttonText'],
                    style:
                        TextStyle(fontSize: 12, color: cardData['buttonColor']),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    List<Map<String, dynamic>> cardData = _generateCardData();

    return Container(
      padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
      child: Row(
        children: List.generate(cardData.length,
            (index) => buildCard(cardData[index], index, context)),
      ),
    );
  }
}
